package com.yanqu.road.server.cmd.activity.twins.cross;

import com.yanqu.road.entity.activity.twins.config.TwinsConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TwinsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsDebugMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_TWINS_CROSS_CHECK_ENTER_ROOM, desc = "")
public class CrossTwinsCheckEnterCmd extends AbstractCommand {

    TwinsProto.CrossTwinsCheckEnterRoomMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = TwinsProto.CrossTwinsCheckEnterRoomMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TwinsConfig config = TwinsMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != reqMsg.getActivityId()) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        List<Long> leaveUserIdList = new ArrayList<>();
        for(long userId : reqMsg.getUserIdList()){
            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
            if(onlinePlayer == null && !TwinsDebugMgr.isDebugUser(userId)){
                leaveUserIdList.add(userId);
            }
        }
        if(leaveUserIdList.size() > 0){
            //拿一个出来判断
            Long userId = leaveUserIdList.get(0);
            int groupId = GameUnionActivityGroupMgr.getGroupId(config.getActivityInfo().getActivityId(), userId);

            TwinsProto.CrossTwinsLeaveRoomMsg.Builder crossMsg = TwinsProto.CrossTwinsLeaveRoomMsg.newBuilder();
            crossMsg.setActivityId(reqMsg.getActivityId());
            crossMsg.addAllUserId(leaveUserIdList);

            GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_TWINS_PVP_LEAVE_BOSS,crossMsg,0,config.getActivityInfo().getActivityId(),groupId);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
